//
//  StylistViewController.m
//  PerfectWorld
//
//  Created by dllo on 16/8/27.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "StylistViewController.h"
#import "StyListTableViewCell.h"
#import "Professionals.h"
#import "ProUserimage.h"
#import "StyListModel.h"
#import "AFNetworking.h"
#import "StylistRightViewController.h"
#import "StylistSonViewController.h"
#import "MJRefresh.h"
#import "LoginLeftViewController.h"
@interface StylistViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)StyListModel *stylistModel;
@property (nonatomic, assign)NSInteger page;
@property (nonatomic, strong)NSMutableArray *arr;
@property (nonatomic, copy)NSString *cityUrl;
@property (nonatomic, strong)UIBarButtonItem *rightBtnItem;

@end

@implementation StylistViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    image.image = [UIImage imageNamed:@"设计师"];
    self.navigationItem.titleView = image;
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBtn.frame = CGRectMake(5 , 0, 30 , 30 );
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"头像"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(clickNavLeftBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.rightBtnItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"全国"] style:UIBarButtonItemStylePlain target:self action:@selector(clickNavRight:)];
    self.navigationItem.rightBarButtonItem = _rightBtnItem;
    self.cityUrl =[_rightBtnItem.title stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [self creatTableView];
    // 下拉刷新, 上拉加载
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 0;
        [self creatHandData];
    }];
    [_tableView.mj_header beginRefreshing];
    _tableView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
        [self creatMJRefreshFoot];
    }];
}
- (void)creatHandData {
    NSString *urlStr = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/user/professionals?start=%ld&count=10&user=(null)&city=%@", (long)_page, _cityUrl];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (_page == 0) {
            _arr = nil;
        }
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:responseObject];
        self.stylistModel = [StyListModel modelWithDic:dic];
        _arr = [NSMutableArray arrayWithArray:_stylistModel.professionals];
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"解析失败");
    }];
}
- (void)creatMJRefreshFoot {
    NSString *urlStr = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/user/professionals?start=%ld&count=10&user=(null)&city=%@", (long)_page, _cityUrl];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:responseObject];
        self.stylistModel = [StyListModel modelWithDic:dic];
        [_arr addObjectsFromArray:_stylistModel.professionals];
        [_tableView reloadData];
        [_tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"解析失败");
    }];
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"StyListTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.bounds.size.height / 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    StyListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.professionals = _arr[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    StylistSonViewController *sonVC = [[StylistSonViewController alloc] init];
    sonVC.urlTitle = _stylistModel.professionals[indexPath.row].userName;
    sonVC.urlHeadImg = _stylistModel.professionals[indexPath.row].userImage.large;
    sonVC.urlID = [_stylistModel.professionals[indexPath.row].userId description];
    sonVC.hidesBottomBarWhenPushed = NO;
    [self.navigationController pushViewController:sonVC animated:YES];
}
- (void)clickNavLeftBtn:(UIBarButtonItem *)leftBtn {
    LoginLeftViewController *loginVC = [[LoginLeftViewController alloc] init];
    [self.navigationController pushViewController:loginVC animated:YES];
}
// 全国按钮关联
- (void)clickNavRight:(UIBarButtonItem *)rightBtn {
    StylistRightViewController *rightVC = [[StylistRightViewController alloc] init];
    rightVC.hidesBottomBarWhenPushed = YES;
    rightVC.cityFilter = ^(NSString *cityName) {
        _rightBtnItem.title = cityName;
        _cityUrl = [_rightBtnItem.title stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [_tableView.mj_header beginRefreshing];
    };
    [self presentViewController:rightVC animated:YES completion:^{
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
